package fd;

import java.util.*;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/*
 * ReentrantLock and condition
 * 
 */
public class Bate2 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("please input n ");
		int n = sc.nextInt();
		FooBar FB = new FooBar(n);
//		FooBar FB = new FooBar(7);
		C1 c1 = new C1(FB);
		C2 c2 = new C2(FB);
		c1.start();
		c2.start();
	}

}

class C1 extends Thread {
	FooBar FB;

	public C1(FooBar FB) {
		this.FB = FB;
	}

	public void run() {
		FB.bar();
	}
}

class C2 extends Thread {
	FooBar FB;

	public C2(FooBar FB) {
		this.FB = FB;
	}

	public void run() {
		FB.foo();
	}
}

class FooBar {
	private int n;
	private ReentrantLock lock = new ReentrantLock(true);
	private Condition condition = lock.newCondition();

	public FooBar(int n) {
		this.n = n;
	}

	public void foo() {
		for (int i = 0; i < n; i++) {
			try {
				lock.lock();
				System.out.print("foo");
			} finally {
				lock.unlock();
			}

		}
	}

	public void bar() {
		for (int i = 0; i < n; i++) {
			try {
				lock.lock();
				System.out.print("bar");
			} finally {
				lock.unlock();
			}

		}
	}
}